/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Form.Validation;

import com.lcfgly.CustomBean.User;
import com.lcfgly.Database.ShopImgs;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户配置表验证
 * @author lcfgly
 */
public class UserValidation {
    
    /**
     * 验证数据
     * @param request
     * @param user
     * @param errorMap
     * @return 
     */
    public static boolean validation(HttpServletRequest request,User user,Map errorMap){
        try {
            String phone="[0-9\\(\\-\\)]*";
            Pattern pregex=Pattern.compile(phone);
            String lg="[0-9]*\\.[0-9]*";
            Pattern lgp=Pattern.compile(lg);
            if(request.getParameter("email")!= null &&!request.getParameter("email").isEmpty()){
                String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
                Pattern regex = Pattern.compile(check);
                Matcher matcher = regex.matcher(request.getParameter("email"));
                if (!matcher.matches()) {
                    errorMap.put("email", "电子邮箱格式有误!请填写正确的邮箱");
                    return false;
                }
                user.getUserinfer().setEmail(request.getParameter("email"));
            }
            if(request.getParameter("phone")!= null &&!request.getParameter("phone").isEmpty()){
                Matcher matcher1=pregex.matcher(request.getParameter("phone"));
                if (!matcher1.matches()) {
                    errorMap.put("phone", "电话号码出现非法字符请检查!");
                    return false;
                }else if(request.getParameter("phone").length()>15){
                    errorMap.put("phone", "电话号码过长请检查是否正确!");
                    return false;
                }
                user.getUserinfer().setPhone(request.getParameter("phone"));
            }
            if(request.getParameter("tel")!= null &&!request.getParameter("tel").isEmpty()){
                Matcher matcher2=pregex.matcher(request.getParameter("tel"));
                if(!matcher2.matches()){
                    errorMap.put("tel", "传真号码出现非法字符请检查!");
                    return false;
                }
                user.getUserinfer().setTel(request.getParameter("tel"));
            }
            if(request.getParameter("lat") != null && !request.getParameter("lat").isEmpty()){
                Matcher matcher=lgp.matcher(request.getParameter("lat"));
                if(matcher.matches()){
                    user.getUserinfer().setLat(request.getParameter("lat"));
                }
            }
             if(request.getParameter("lng") != null && !request.getParameter("lng").isEmpty()){
                Matcher matcher=lgp.matcher(request.getParameter("lng"));
                if(matcher.matches()){
                    user.getUserinfer().setLng(request.getParameter("lng"));
                }
            }
            if(request.getParameter("qq") != null && !request.getParameter("qq").isEmpty()){
                String qq="^[0-9]*$";
                Pattern pq=Pattern.compile(qq);
                Matcher mq=pq.matcher(request.getParameter("qq"));
                if(mq.matches()){
                    user.getUserinfer().setQq(request.getParameter("qq"));
                }else{
                    errorMap.put("error", "QQ号存在异常字符!");
                    return false;
                }
            }
        } catch (Exception e) {
            errorMap.put("error", "错误请检查输入的数据内容");
            return false;
        }
        user.getUsers().setUname(request.getParameter("uname"));
        user.getUserinfer().setAddress(request.getParameter("address"));
        if(request.getParameter("cityid")!= null && !request.getParameter("cityid").isEmpty()){
            if(request.getParameter("ipcode")!= null && !request.getParameter("ipcode").isEmpty())
                user.getUserinfer().setIpcode(Integer.valueOf(request.getParameter("ipcode")));
            user.getUserinfer().setCityid(Integer.valueOf(request.getParameter("cityid")));
        }else{
            if(request.getParameter("ipcode")!= null && !request.getParameter("ipcode").isEmpty())
                user.getUserinfer().setCityid(Integer.valueOf(request.getParameter("ipcode")));
            if(request.getParameter("prcid")!= null && !request.getParameter("prcid").isEmpty())
                user.getUserinfer().setIpcode(Integer.valueOf(request.getParameter("prcid")));
        }
        List<ShopImgs> imgs=user.getImgs();
        if(request.getParameter("litpic1")!= null && !request.getParameter("litpic1").isEmpty())
            imgs.get(0).setImgurl(request.getParameter("litpic1"));
        if(request.getParameter("litpic2")!= null && !request.getParameter("litpic2").isEmpty())
            imgs.get(1).setImgurl(request.getParameter("litpic2"));
        if(request.getParameter("litpic3")!= null && !request.getParameter("litpic3").isEmpty())
            imgs.get(2).setImgurl(request.getParameter("litpic3"));
        return true;
    }
    
}
